package priority_queue.compare;

import java.util.Arrays;
import java.util.Comparator;


class StudentSec implements Comparator<Student>{

    @Override
    public int compare(Student o1, Student o2) {
        return o1.getAge()-o2.getAge();
    }
}

class StudentDesc implements Comparator<Student>{

    @Override
    public int compare(Student o1, Student o2) {
        return o2.getAge()-o1.getAge();
    }
}

public class Student  {
    private int age;
    private  String name;

    public int getAge() {
        return age;
    }

    public Student(int age, String name) {
        this.age = age;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }

//    @Override
//    public int compareTo(Student o) {
//      return this.age-o.age;
//    }

    public static void main(String[] args) {
        Student[] arr=new Student[]{
                new Student(19,"张三"),
                new Student(20,"李四"),
                new Student(18,"王麻子")
        };
        //此时Student不具备比较的能力，但是可以通过比较器去比较
        Arrays.sort(arr,new StudentSec());
        System.out.println(Arrays.toString(arr));
        Arrays.sort(arr,new StudentDesc());
        System.out.println(Arrays.toString(arr));
    }
}
